package com.gewu.ordlelist.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityOptionsCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.gewu.ordlelist.R;
import com.gewu.ordlelist.activity.moreActivity;
import com.gewu.ordlelist.activity.search_activity;
import com.gewu.ordlelist.base.BaseFragment;
import com.gewu.ordlelist.database.dbbean.ByCar;
import com.gewu.ordlelist.database.dbdao.ByCarDao;
import com.gewu.ordlelist.utils.jocUtils;

import java.util.List;
import java.util.Random;

public class twoFragment extends BaseFragment {


    @Override
    public View FindView(LayoutInflater inflater) {
        return LayoutInflater.from(getContext()).inflate(R.layout.two_fragment, null, false);
    }

    @Override
    public void initView(View view) {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        TextView s =  view.findViewById(R.id.sousuo);
        TextView text = (TextView) view.findViewById(R.id.text);
        RecyclerView rec = (RecyclerView) view.findViewById(R.id.rec);
        ImageView image = (ImageView) view.findViewById(R.id.image);
        toolbar.setTitle(setToolbarText());

        s.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), search_activity.class);
                ActivityOptionsCompat optionsCompat =
                        ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), s, "sousuo");
                startActivity(intent,optionsCompat.toBundle());
            }
        });


        rec.setLayoutManager(new GridLayoutManager(getContext(), 2));
        rec.setAdapter(new RecyclerView.Adapter() {

            class OneItem extends RecyclerView.ViewHolder {

                ImageView imageView;
                TextView textView;
                Button buadd;

                public OneItem(View inflate) {
                    super(inflate);
                    imageView = inflate.findViewById(R.id.image);
                    textView = inflate.findViewById(R.id.text);
                    buadd = inflate.findViewById(R.id.buadd);
                }
            }

            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.two_item, parent, false);
                return new OneItem(inflate);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                RoundedCorners roundedCorners = new RoundedCorners(100);
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                Glide.with(holder.itemView).load(jocUtils.getimgs()[new Random().nextInt(6)]).apply(options).into(((OneItem) holder).imageView);
                ((OneItem) holder).textView.setText(jocUtils.getnames()[position]);
                ((OneItem) holder).imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(((OneItem) holder).imageView.getContext(), moreActivity.class);
                        intent.putExtra("index",position);
                        intent.putExtra("name",((OneItem) holder).textView.getText().toString());
                        ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), ((OneItem) holder).imageView, "image");
                        startActivity(intent,optionsCompat.toBundle());
                    }
                });
                ((OneItem) holder).buadd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ByCarDao byCarDao = new ByCarDao(holder.itemView.getContext());
                        List<ByCar> byCars = byCarDao.querLike(((OneItem) holder).textView.getText().toString());
                        if (!byCars.isEmpty()) {
                            AlertDialog.Builder builder = jocUtils.showDilog(holder.itemView.getContext(), "提示！", "已经添加！");
                            builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                        } else {
                            ByCar byCar = new ByCar();
                            byCar.setName(((OneItem) holder).textView.getText().toString());
                            byCar.setMoney(String.valueOf(new Random().nextInt(1000)));
                            byCar.setNum(1);
                            byCarDao.add(byCar);
                            AlertDialog.Builder builder = jocUtils.showDilog(holder.itemView.getContext(), "提示！", "成功添加！");
                            builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                        }
                    }
                });
            }

            @Override
            public int getItemCount() {
                return jocUtils.getnames().length;
            }
        });
    }

    @Override
    public String setToolbarText() {
        return "AddFooter";
    }

    @Override
    public void setAdapter() {

    }
}
